var QQMapWX = require('../libs/qqmap-wx-jssdk.js');
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    siteName: '', //打包站名称
    name: '', //联系人名称
    currentLat: '',
    currentLon: '',
    address: '',
    detailAddress: '',
    location: '',
    timer: '',
    UserData: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    let data = options.data;
    console.log(JSON.parse(data));
    that.setData({ UserData: JSON.parse(data) });
    that.getLocationData();
  },

  //获取当前经纬度
  getLocationData: function() {
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      // type: 'wgs84',
      success: function(res) {
        var latitude = res.latitude;
        var longitude = res.longitude;
        var locationDic = { latitude: latitude, longitude: longitude };
        wx.setStorage({
          key: 'map_Location',
          data: locationDic
        });
        that.setData({
          currentLat: latitude,
          currentLon: longitude
        });

        var qqmapsdk = new QQMapWX({
          key: 'UUPBZ-T3H3D-EQF45-P3QBF-MKCAE-IOFLX'
        });
        // 调用接口
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: that.data.currentLat,
            longitude: that.data.currentLon
          },
          success: function(res) {
            //console.log(res);
          },
          fail: function(res) {
            //console.log(res);
          },
          complete: function(res) {
            var address =
              res.result.address_component.province +
              ' ' +
              res.result.address_component.city +
              ' ' +
              res.result.address_component.district;
            var detailAddress = res.result.address_component.street;
            var location =
              res.result.location.lat + ',' + res.result.location.lng;
            that.setData({
              address: address,
              detailAddress: detailAddress,
              location: location
            });
          }
        });
      }
    });
  },

  // 选择省市区
  chooseAddress: function(e) {
    wx.navigateTo({
      url: '../mine/mapLocation/searchMapLocation/searchMapLocation'
    });
  },

  //提交按钮
  bindFormSubmit: function(e) {
    var that = this;
    var siteName = e.detail.value.siteName;
    var name = e.detail.value.name;
    var address = e.detail.value.address;
    var detailAddress = e.detail.value.detailAddress;
    var province = that.data.address.split(' ')[0];
    var city = that.data.address.split(' ')[1];
    var area = that.data.address.split(' ')[2];
    var lat = that.data.location.split(',')[0];
    var lng = that.data.location.split(',')[1];
    if (!address || !detailAddress || !siteName || !name) {
      wx.showToast({
        title: '各项信息必填',
        icon: 'none'
      });
      return false;
    } else {
      that.setData({ buthidden: true });
      wx.showLoading({ title: '提交中', mask: true });
      wx.request({
        method: 'POST',
        url: app.preUrl + 'toVerifyCode',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded;'
        },
        data: {
          userType: 2,
          telephone: that.data.UserData.telephone,
          password: that.data.UserData.password,
          code: that.data.UserData.code,
          siteName: siteName,
          contacts: name,
          province: province,
          city: city,
          area: area,
          address: detailAddress,
          lat: lat,
          lng: lng
        },
        success: function(res) {
          wx.hideLoading();
          if (res.data.code == 100) {
            var userId = res.data.result.id;
            wx.setStorageSync('userId', userId);
            app.globalData.userId = userId;
            wx.showToast({
              title: '注册成功',
              icon: 'success',
              duration: 1000
            });
            setTimeout(function() {
              wx.reLaunch({
                url: '../home/home'
              });
            }, 1500);
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.message,
              confirmColor: '#56BDE0',
              showCancel: false
            });
            that.setData({ buthidden: false });
          }
        },
        fail: function(error) {
          that.setData({ buthidden: false });
        }
      });
    }
  },

  onUnload() {
    //页面卸载  当redirectTo或navigateBack的时候调用
    clearTimeout(this.data.timer);
  }
});
